#include <iostream>
#include <string>
#include <memory>
#include <mutex>
#include <fstream>
#include <jsoncpp/json/json.h>
#include "jsonUtil.hpp"

namespace bucai
{
    class Config
    {
    private:
        int _hot_time;
        int _server_port;
        std::string _server_ip;
        std::string _url_prefix;
        std::string _arc_suffix;
        std::string _pack_dir;
        std::string _back_dir;
        std::string _manager_file;

        Json::Value _value;
        std::string _filename;
        static std::mutex _mutex;
        static Config *_myjson;

    private:
        Config(std::string name) : _filename(name)
        {
            std::string str1;
            std::ifstream ifs;
            ifs.open(_filename);
            ifs.seekg(0, std::ios::end);
            size_t sz = ifs.tellg(); // 计算文件大小
            ifs.seekg(0, std::ios::beg);

            str1.resize(sz);
            ifs.read(&str1[0], sz);
            ifs.close();
            JsonUtil json;
            json.Serialize(_value, &str1);

            _hot_time = _value["hot_time"].asInt();
            _server_port = _value["server_port"].asInt();
            _server_ip = _value["server_ip"].asString();
            _url_prefix = _value["url_prefix"].asString();
            _arc_suffix = _value["arc_suffix"].asString();
            _pack_dir = _value["pack_dir"].asString();
            _back_dir = _value["back_dir"].asString();
            _manager_file = _value["manager_file"].asString();
        }

    public:
        static Config *getConfig(std::string name) //懒汉单例获取
        {
            if (_myjson == nullptr)
            {
                _mutex.lock();
                if (_myjson == nullptr)
                {
                    _myjson = new Config(name);
                }
                _mutex.unlock();
            }
            return _myjson;
        }

    public:
        int GetHotTime(){
            return _hot_time;
        }
        int GetServerPort(){
            return _server_port;
        }
        std::string GetServerIp(){
            return _server_ip;
        }
        std::string GetURLPrefix(){
            return _url_prefix;
        }
        std::string GetArcSuffix(){
            return _arc_suffix;
        }
        std::string GetPackDir(){
            return _pack_dir;
        }
        std::string GetBackDir(){
            return _back_dir;
        }
        std::string GetManagerFile(){
            return _manager_file;
        }
    };
    Config* Config::_myjson = nullptr;
    std::mutex Config::_mutex;
}

int main()
{

    bucai::Config* con = bucai::Config::getConfig("./httplib.json");

    std::cout << con->GetArcSuffix() << std::endl;
    std::cout << con->GetBackDir() << std::endl;
    std::cout << con->GetHotTime() << std::endl;
    std::cout << con->GetManagerFile() << std::endl;
    std::cout << con->GetPackDir() << std::endl;
    std::cout << con->GetServerIp() << std::endl;
    std::cout << con->GetServerPort() << std::endl;
    std::cout << con->GetURLPrefix() << std::endl;

    return 0;
}